package com.java.tutorials.jdk7.fileattribute;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Set;
/**
 * Created by xuzhike on 2016/1/24.
 * http://www.java2s.com/Code/Java/JDK-7/RemovepermissionforPosixFile.htm
 * Remove permission for Posix File
 */
public class RemovepermissionforPosixFile {
    public static void main(String[] args) throws Exception {
        Path path = Paths.get("home/docs/users.txt");
        removePermission(path, PosixFilePermission.OWNER_EXECUTE);
    }

    private static void removePermission(Path path, PosixFilePermission permission)
            throws Exception {
        System.out.println("\nRemoving permission for " + path.getFileName());
        PosixFileAttributeView view = Files.getFileAttributeView(path,
                PosixFileAttributeView.class);

        PosixFileAttributes attributes = view.readAttributes();

        Set<PosixFilePermission> permissions = attributes.permissions();
        permissions.remove(permission);

        view.setPermissions(permissions);
        System.out.println();
    }
}
